package cn.gok.hotelback.mapper;

import cn.gok.hotelback.entity.RoomOrder;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
import cn.gok.hotelback.entity.OrderReview;
import org.apache.ibatis.annotations.Delete;

@Mapper
public interface RoomOrderMapper {
    @Insert("INSERT INTO room_orders (order_id, user_id, username, contact_phone, order_status, check_in_time, room_type_id, room_id, room_number, guest_info, total_amount) " +
            "VALUES (#{orderId}, #{userId}, #{username}, #{contactPhone}, #{orderStatus}, #{checkInTime}, #{roomTypeId}, #{roomId}, #{roomNumber}, #{guestInfo}, #{totalAmount})")
    int insertOrder(RoomOrder order);

    @Select("SELECT * FROM room_orders WHERE RIGHT(contact_phone, 4) = #{suffix} AND order_status = '0'")
    List<RoomOrder> selectByPhoneSuffix(@Param("suffix") String suffix);

    @Update("UPDATE room_orders SET order_status='1', room_id=#{roomId}, room_number=#{roomNumber}, guest_info=#{guestInfo} WHERE order_id=#{orderId}")
    int updateCheckin(@Param("orderId") String orderId, @Param("roomId") Integer roomId, @Param("roomNumber") String roomNumber, @Param("guestInfo") String guestInfo);

    @Select("SELECT * FROM room_orders WHERE room_number = #{roomNumber} AND order_status = '1' LIMIT 1")
    RoomOrder selectCheckedInOrderByRoomNumber(@Param("roomNumber") String roomNumber);

    @Update("UPDATE room_orders SET order_status = '2' WHERE order_id = #{orderId}")
    int updateCheckout(@Param("orderId") String orderId);

    @Select("SELECT * FROM room_orders WHERE user_id = #{userId}")
    List<RoomOrder> findOrdersByUserId(@Param("userId") Integer userId);

    @Insert("INSERT INTO order_reviews (order_id, user_id, username, rating, review_text) VALUES (#{orderId}, (SELECT user_id FROM room_orders WHERE order_id = #{orderId}), (SELECT username FROM room_orders WHERE order_id = #{orderId}), #{rating}, #{content})")
    int insertReview(@Param("orderId") String orderId, @Param("rating") Integer rating, @Param("content") String content);

    @Select("SELECT * FROM order_reviews WHERE user_id = #{userId} ORDER BY review_id ASC")
    List<OrderReview> getUserReviews(@Param("userId") Integer userId);

    @Insert("INSERT INTO order_reviews (order_id, user_id, username, rating, review_text) VALUES (#{orderId}, (SELECT user_id FROM room_orders WHERE order_id = #{orderId}), (SELECT username FROM room_orders WHERE order_id = #{orderId}), 0, #{content})")
    int appendReview(@Param("orderId") String orderId, @Param("content") String content);

    @Update("UPDATE order_reviews SET rating = #{rating}, review_text = #{content} WHERE review_id = #{reviewId}")
    int updateReview(@Param("reviewId") Integer reviewId, @Param("rating") Integer rating, @Param("content") String content);

    @Delete("DELETE FROM order_reviews WHERE review_id = #{reviewId}")
    int deleteReview(@Param("reviewId") Integer reviewId);

    @Select("SELECT order_id FROM order_reviews WHERE review_id = #{reviewId}")
    String getOrderIdByReviewId(@Param("reviewId") Integer reviewId);

    @Delete("DELETE FROM order_reviews WHERE order_id = #{orderId}")
    int deleteReviewsByOrderId(@Param("orderId") String orderId);

    @Select("SELECT * FROM room_orders")
    java.util.List<RoomOrder> selectAllOrders();

    @Select("SELECT * FROM order_reviews WHERE order_id = #{orderId} LIMIT 1")
    OrderReview selectReviewByOrderId(@Param("orderId") String orderId);

    @Select("SELECT * FROM order_reviews ORDER BY review_id DESC")
    java.util.List<OrderReview> selectAllReviews();
} 